package com.example.administrator.mynote;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * 首页:
 * 1、图片的点击事件
 * 2、ListView：
 *    1）默认数据展示
 *    2）列表项的点击事件
 *    3）列表项的长按事件
 */
public class NoteActivity extends AppCompatActivity implements View.OnClickListener,AdapterView.OnItemClickListener,AdapterView.OnItemLongClickListener {
    ListView listView;
    ImageView image_add;
    ArrayList<NoteBean> arrayList;
    NoteSQLiteOpenHelper helper;
    NoteAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_note);
        listView=findViewById(R.id.note_listview);
        image_add=findViewById(R.id.note_image_add);
        /*图片 绑定点击事件*/
        image_add.setOnClickListener(this);
        helper=new NoteSQLiteOpenHelper(this);
        showData();
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
    }

    private void showData() {
        if (arrayList!=null){
            arrayList.clear();
        }
        arrayList=helper.select();
        adapter=new NoteAdapter(this,arrayList);
        listView.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        Intent intent =new Intent(this,RecordActivity.class);
        /*跳转*/
        startActivityForResult(intent,1);
    }

    /*自动调用 当B页面返回到A页面 自动刷新ListView（数据重新展示）*/
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data){
        super.onActivityResult(requestCode,resultCode,data);
        if (requestCode==1&&resultCode==2){
            showData();
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        NoteBean noteBean=arrayList.get(position);
        Intent intent=new Intent(this,RecordActivity.class);
        intent.putExtra("id",noteBean.getId());
        intent.putExtra("content",noteBean.getContent());
        intent.putExtra("time",noteBean.getTime());
        startActivityForResult(intent,1);
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {


        return true;
    }
}
